﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;

namespace BAL
{
    public partial class DeliveryStk : Form
    {
        private LoginService.BarcodeJudgeServices bjs = new BAL.LoginService.BarcodeJudgeServices();
        private string DInterID;
        private string Did;
        private string PkgsBarCode;
        private string SKU;

        public DeliveryStk()
        {
            InitializeComponent();
        }

        public DeliveryStk(string ID,string fno, string Sku)
        {
            InitializeComponent();
            DInterID = ID;
            Did = fno;
            SKU = Sku;
           
        }
        //获取数据
        private void BinData()
        {
            DataSet ds = bjs.GetDeliveryStk(Did);
            DataTable dt = new DataTable();

            dt = ds.Tables[0];
            if (dt.Rows.Count <= 0)
            {
                return;
            }
            else
            {
                dataGrid1.DataSource = dt;
            }
        }
        //数据窗口样式
        public void DataGridStyle()
        {
            DataGridTableStyle dts = new DataGridTableStyle();
            dataGrid1.TableStyles.Clear();
            dts.MappingName = "DeliveryStk";

            DataGridTextBoxColumn cl1 = new DataGridTextBoxColumn();
            cl1.HeaderText = "序号";
            cl1.MappingName = "Num";
            cl1.Width = 30;
            dts.GridColumnStyles.Add(cl1);

            DataGridTextBoxColumn cl2 = new DataGridTextBoxColumn();
            cl2.HeaderText = "包装码";
            cl2.MappingName = "PkgsID";
            cl2.Width = 40;
            dts.GridColumnStyles.Add(cl2);

            DataGridTextBoxColumn cl3 = new DataGridTextBoxColumn();
            cl3.HeaderText = "SKU";
            cl3.MappingName = "ItemSku";
            cl3.Width = 100;
            dts.GridColumnStyles.Add(cl3);

            DataGridTextBoxColumn cl4 = new DataGridTextBoxColumn();
            cl4.HeaderText = "根长";
            cl4.MappingName = "StickLen";
            cl4.Width = 40;
            dts.GridColumnStyles.Add(cl4);

            DataGridTextBoxColumn cl5 = new DataGridTextBoxColumn();
            cl5.HeaderText = "根数";
            cl5.MappingName = "Stick";
            cl5.Width = 40;
            dts.GridColumnStyles.Add(cl5);

            DataGridTextBoxColumn cl6 = new DataGridTextBoxColumn();
            cl6.HeaderText = "总长";
            cl6.MappingName = "FQty";
            cl6.Width = 40;
            dts.GridColumnStyles.Add(cl6);

            DataGridTextBoxColumn cl7 = new DataGridTextBoxColumn();
            cl7.HeaderText = "仓位名";
            cl7.MappingName = "FName";
            cl7.Width = 80;
            dts.GridColumnStyles.Add(cl7);

            dataGrid1.TableStyles.Add(dts);

        }
        //赋值
        public void BinValue() 
        {
            string DInterID = ""; string Dnumber = ""; string NDnumber = "";
            bool lb = bjs.GetDeliveryById(Did, out DInterID, out Dnumber, out NDnumber);

            if (lb) 
            {
                this.tb_DInterID.Text = DInterID;
                this.tb_Dnumber.Text = Dnumber;
                this.tb_NDnumber.Text = NDnumber;
                this.tb_Did.Text = Did;

                this.tb_PkgsBarCode.Text = "";
                this.tb_PkgsID.Text = "";
                this.tb_FQty.Text = "";
                this.tb_FNumber.Text = "";
            }
            
        }

        private void DeliveryStk_Load(object sender, EventArgs e)
        {
            this.tb_PkgsBarCode.Focus();

            if (Did == "" || Did == null) 
            {
            } 
            else 
            {
                DataGridStyle();
                BinData();
                BinValue();
            }
        }
        //刷新
        private void button3_Click(object sender, EventArgs e)
        {
            DataGridStyle();
            BinData();
            BinValue();
        }
        //返回
        private void button2_Click(object sender, EventArgs e)
        {
            Delivery del = new Delivery(DInterID);
            del.Show();
            this.Hide();
        }

        private void tb_PkgsBarCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                PkgsBarCode = this.tb_PkgsBarCode.Text;
                string PkgsID = ""; string ItemSku = ""; string StickLen = "";
                string Stick = ""; string FQty = ""; string FNumber = "";
                string PkgsStatus = "";

                bool lb_return = bjs.GetPackage(PkgsBarCode, out  PkgsID, out  ItemSku, out  StickLen, out  Stick, out  FQty, out  FNumber, out  PkgsStatus);
        

                if (lb_return)
                {
                    if (ItemSku.Trim() == SKU.Trim())
                    {
                        this.tb_FQty.Text = FQty;
                        this.tb_FNumber.Text = FNumber;
                        this.tb_PkgsID.Text = PkgsID;
                        this.button1.Focus();
                    }
                    else 
                    {
                        MessageBox.Show("SKU不符！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("仓位码不存在！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }

            }

        }
        //提交
        private void button1_Click(object sender, EventArgs e)
        {
            string PkgsID = this.tb_PkgsID.Text;
            bool lb = false;
            if (Did == "" || Did == null)
            {
            }
            else 
            {
                if (PkgsID == "" || PkgsID == null)
                {
                    MessageBox.Show("请扫描包装码！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                    return;
                }
                else 
                {
                    lb = bjs.UpdateDeliveryStk(PkgsID, Did);
                    if (lb)
                    {
                        DataGridStyle();
                        BinData();
                        BinValue();
                    }
                    else 
                    {
                        MessageBox.Show("提交失败！", "警告！", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button3);
                        return;
                    }
                }
            }
        }




    }
}